/**   
 * @Title: MenuAction.java 
 * @Package com.foreveross.smsh.models.menu.web 
 * @Description: TODO 
 * @author lixiaodong lixiaodong@foreveross.com  
 * @date 2016年3月7日 下午4:30:42 
 * @version V1.0   
 */
package com.foreveross.smsh.models.menu.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.smsh.core.pojo.TreeNode;
import com.foreveross.smsh.core.web.ModelAction;
import com.foreveross.smsh.models.menu.pojo.Menu;
import com.foreveross.smsh.models.menu.service.MenuService;

/** 
 * @ClassName: MenuAction 
 * @Description: TODO
 * @author A18ccms a18ccms_gmail_com 
 * @date 2016年3月7日 下午4:30:42 
 *  
 */
@Controller
@RequestMapping("/menu")
public class MenuAction extends ModelAction<Menu>{
	private static final Logger logger = LogManager.getLogger(ModelAction.class);
	@Autowired
	private MenuService menuService;
	@RequestMapping(method=RequestMethod.GET)
	public String list(){
		return "models/menu/menu-list";
	}
	
	@RequestMapping(value="/query",method=RequestMethod.POST)
	@ResponseBody
	public void query(){
		List<Menu> menuRoots =getRootMenu();
		logger.info(menuRoots);
		List<TreeNode> treeNodeRoots = new ArrayList<TreeNode>();
		for (Menu menu : menuRoots) {
			List<Menu> menus = getChildrenNodes(menu.getId());
			TreeNode treeNode = convert(menu);
			treeMenuList(treeNode, menus);
			treeNodeRoots.add(treeNode);
		}
		this.printJson(treeNodeRoots);
	}
	
	/**
	 * 将organ对象转换为treenode对象
	 * @param organ
	 * @return
	 */
	private TreeNode convert(Menu menu) {
		TreeNode node=new TreeNode();
		node.setId(menu.getId());
		node.setText(menu.getName());
		if(menu.getType()==0){
			node.setState(TreeNode.STATE_CLOSED);
		}else{
			node.setState(TreeNode.STATE_OPEN);
		}
		node.getAttributes().put("url", menu.getUrl());
		node.getAttributes().put("parentId", menu.getParentId());
		node.getAttributes().put("type", menu.getType());
		return node;
	}
	
	
	/**
	 * 
	 * @Title: treeMenuList 
	 * @Description: 组装树对象
	 * @param  parentNode
	 * @param  organs    
	 * @return void    
	 * @throws
	 */
	public void treeMenuList(TreeNode parentNode, List<Menu> menus) {
		for (Menu menu : menus) {
			TreeNode treeNode = convert(menu);
			if (parentNode.getId().equals(menu.getParentId())) {
				treeMenuList(treeNode, menus);
				parentNode.getChildren().add(treeNode);
			}
		}
	}
	
	 /**
	  * 
	  * @Title: getChildrenNodes 
	  * @Description: 查询根部门下所有组织机构
	  * @param  parentid
	  * @param     
	  * @return List<Organ>    
	  * @throws
	  */
	 private  List<Menu> getChildrenNodes(String parentId) {
		  return menuService.get("parentId", parentId);
	  }
	
	 /**
	  * 
	  * @Title: getChildNodes 
	  * @Description: TODO
	  * @param  parentid 通过父节点查询一级子节点
	  * @return List<Organ>    
	  * @throws
	  */
	 @SuppressWarnings("unchecked")
	private List<TreeNode> getChildNodes(String parentId){
		 List<Menu> menus =menuService.get("parentId", parentId);
		 List<TreeNode> treeChildren = new ArrayList<TreeNode>();
		 for (Menu menu : menus) {
			 TreeNode treeNode =  convert(menu);
			 treeChildren.add(treeNode);
		}
		 return treeChildren;
	 }
	 
	/**
	 * 
	 * @Title: getRootOrgan 
	 * @Description: 获取根组织机构
	 * @param  organid
	 * @param     
	 * @return List<TreeNode>    
	 * @throws
	 */
	public List<Menu> getRootMenu(){
		return menuService.get("parentId", null);
	}
	

}
